import { _decorator, CircleCollider2D, Collider, Collider2D, Component, Contact2DType, EventTouch, IPhysics2DContact, Label, math, Node, RigidBody2D, Size, tween, UITransform, v3, Vec2 } from 'cc';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('FleeItem')
export class FleeItem extends Component {

    @property(Label)
    RadiusLabel: Label = null;

    @property(Label)
    EndLabel: Label = null;

    @property(Node)
    Down: Node = null;

    @property
    StartRadius: number = 50;

    @property
    MinRadius: number = 50;

    @property
    Sub: number = 1;

    @property(Node)
    AredNode: Node = null;

    @property(Node)
    StartNode: Node = null;

    public RigidBody: RigidBody2D = null;

    private _collider: CircleCollider2D = null;
    private _UITransform: UITransform = null;
    private _curRadius: number = 0;
    private _downY: number = 0;
    private _isPause: boolean = false;
    private _curLinearVelocity: Vec2 = new Vec2();

    protected onLoad(): void {
        this.RigidBody = this.getComponent(RigidBody2D);
        this._collider = this.node.getComponent(CircleCollider2D);
        this._UITransform = this.node.getComponent(UITransform);
        this._downY = this.Down.getWorldPosition().clone().y;

        if (this._collider) {
            this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }

        // this.RigidBody.linearVelocity = Vec2.ZERO;
        this.RigidBody.gravityScale = 0;

        this.AredNode.once(Node.EventType.TOUCH_START, this.onTouchStart, this);
        tween(this.StartNode)
            .to(1, { scale: v3(1.2, 1.2, 1.2) }, { easing: `sineIn` })
            .to(1, { scale: v3(1, 1, 1) }, { easing: `sineIn` })
            .union()
            .repeatForever()
            .start();
    }

    protected update(dt: number): void {
        if (this._isPause) return;
        if (this.EndLabel.node.active == true) return;
        if (this.node.getWorldPosition().y < this._downY) {
            this.EndLabel.node.active = true;
            this.EndLabel.string = `${this.node.name}获胜！`;
        }
    }

    protected start(): void {
        this._curRadius = this.StartRadius;
        this.changeItem();
    }

    changeItem() {
        const size: Size = new Size(this._curRadius * 2, this._curRadius * 2);
        this._UITransform.setContentSize(size);
        this._collider.radius = this._curRadius;
        // if (this._curRadius <= this.MinRadius) {
        //     this._collider.restitution = 0.95
        // }
        this._collider.restitution -= 0.001;

        this.scheduleOnce(() => { this._collider.apply(); })
        this.RadiusLabel.string = `半径：${this._curRadius}`;
    }


    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (this._curRadius <= this.MinRadius) return;
        if (otherCollider.group == 1 << 2) {
            this._curRadius = math.clamp(this._curRadius - this.Sub, this.MinRadius, this.StartRadius);
            this.changeItem();
        }
    }

    private pause() {
        this._isPause = !this._isPause;
        if (this._isPause) {
            this._curLinearVelocity = this.RigidBody.linearVelocity;
            this.RigidBody.linearVelocity = Vec2.ZERO;
            this.RigidBody.gravityScale = 0;
        } else {
            this.RigidBody.gravityScale = 3;
            this.RigidBody.linearVelocity = this._curLinearVelocity;
        }
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }

    onTouchStart(event: EventTouch) {
        this.AredNode.active = false;
        this.RigidBody.gravityScale = 3;
        this.RigidBody.wakeUp();
    }
}


